﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace LinkCard.AdminCP.Deal
{
    public partial class deal_all_published : System.Web.UI.Page
    {
        public string sName = "";
        public string sPostDate = "";
        public decimal nPrice = 0;
        public Int64 nCategoryID = 0;
        public Int64 nSupplierID = 0;


        private int _totalRecords;
        private int nPageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Name"] != null) { sName = Request.QueryString["Name"].Trim(); }
                if (Request.QueryString["PostDate"] != null) { sPostDate = Request.QueryString["PostDate"].Trim(); }
                if (Request.QueryString["Price"] != null) { nPrice =ConvertUtility.ToDecimal( Request.QueryString["Price"].Trim()); }
                if (Request.QueryString["SupplierID"] != null) { nSupplierID =ConvertUtility.ToInt64( Request.QueryString["SupplierID"].Trim()); }

                if (Request.QueryString["CategoryID"] != null) { nSupplierID = ConvertUtility.ToInt64(Request.QueryString["CategoryID"].Trim()); }


                DataBindPaging();
                loadFormSearch();
            }

        }

        private void DataBindPaging()
        {

            try
            {
                string sFilter = "AND Deal.Status = 2";
                if (sName != "" && sName != "Tên") sFilter = sFilter + " AND Deal.Name like N'%" + sName + "%'";

                if (sPostDate != "" && sPostDate != "Ngày đăng") sFilter = sFilter + " AND Deal.PostDate = '" + sPostDate + "'";

                if (nPrice > 0) sFilter = sFilter + " AND Deal.Price ="+nPrice.ToString();

                if (nCategoryID > 0) sFilter = sFilter + " AND Deal.CategoryID =" + nCategoryID.ToString();
                if (nSupplierID > 0) sFilter = sFilter + " AND Deal.SupplierID =" + nSupplierID.ToString();

                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }
                
                

                DataTable dt = DataHelper.LoadPageInSQL("DealList", curpage, nPageSize, ref _totalRecords, new SqlParameter("@Filter", sFilter));

                if (dt.Rows.Count > 0)
                {
                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }
                    
                    if (nPageCount < 2) divPager.Visible = false;
                    
                    PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);
                    
                    msgList.DataSource = dt;
                    
                    msgList.DataBind();
                    msgNoData.Visible = false;

                }
                
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["DealID"].ToString();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int nAction = int.Parse(ActionOption.Value);
            if (nAction == 1)
            {
                string sArray = "(0";

                foreach (RepeaterItem item in msgList.Items)
                {
                    var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                    if (chkDelete.Checked)
                    {
                        sArray = sArray + "," + chkDelete.Value + "";
                    }
                }
                sArray = sArray + ")";

                string sSQL = "delete deal where dealID in" + sArray;
                DataHelper.ExecuteNonQuery(sSQL, true);
                Response.Redirect("/deal/deal_all_published.aspx");
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            sName = Name.Value.Trim();
            sPostDate = PostDate.Value.Trim();
            nPrice = ConvertUtility.ToDecimal( Price.Value.Trim());
            nCategoryID = Int64.Parse(CategoryID.SelectedValue);
            nSupplierID = Int64.Parse(SupplierID.SelectedValue);
            DataBindPaging();
            loadFormSearch();
        }

        protected void loadFormSearch()
        {
            // Category Option 
            DataTable dt;
            dt = DataHelper.SelectTable("SELECT CategoryID, [Text] FROM Category where [level] = 2 ORDER BY [Text]", false);
            CategoryID.DataSource = dt;
            CategoryID.DataTextField = "Text";
            CategoryID.DataValueField = "CategoryID";
            CategoryID.DataBind();
            CategoryID.Items.Insert(0, new ListItem("Danh mục", "0"));

            CategoryID.SelectedValue = nCategoryID.ToString();


            // Supplier Option 

            dt = DataHelper.SelectTable("SELECT SupplierID, Name FROM Supplier ORDER BY Name", false);
            SupplierID.DataSource = dt;
            SupplierID.DataTextField = "Name";
            SupplierID.DataValueField = "SupplierID";
            SupplierID.DataBind();
            SupplierID.Items.Insert(0, new ListItem("Công ty bán hàng", "0"));

            SupplierID.SelectedValue = nCategoryID.ToString();

        }


    }
}